package cn.tedu.blackhorse.controller;

import cn.tedu.blackhorse.pojo.dto.AdminAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.blackhorse.pojo.dto.AdminUpdateDTO;
import cn.tedu.blackhorse.pojo.dto.AdminUpdatePasswordDTO;
import cn.tedu.blackhorse.pojo.vo.AdminListItemVO;
import cn.tedu.blackhorse.service.IAdminService;
import cn.tedu.blackhorse.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags = "管理员管理模块")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 88)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO){
        log.debug("开始管理员登录");
        String jwt = adminService.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

    @GetMapping("")
    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 100)
    public JsonResult<List<AdminListItemVO>> list(){
        log.debug("开始处理查询管理员列表业务");
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/addNew")
    @ApiOperation("新增管理员")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理添加管理员业务");
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> deleteById(Long id){
        log.debug("开始处理删除管理员业务");
        adminService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 400)
    public JsonResult<Void> enableById(@PathVariable Long id){
        log.debug("开始处理启用管理员业务");
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 450)
    public JsonResult<Void> disableById(@PathVariable Long id){
        log.debug("开始处理禁用管理员业务");
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @PostMapping("/setPassword")
    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 500)
    public JsonResult<Void> setPassword(AdminUpdatePasswordDTO adminUpdatePasswordDTO){
        log.debug("开始处理修改管理员密码业务");
        adminService.setPassword(adminUpdatePasswordDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/setData")
    @ApiOperation("修改详情")
    @ApiOperationSupport(order = 500)
    public JsonResult<Void> setData(@PathVariable Long id, AdminUpdateDTO adminUpdateDTO){
        log.debug("开始处理修改管理员详情业务");
        adminService.setData(id,adminUpdateDTO);
        return JsonResult.ok();
    }

}
